package org.jeecg.modules.basicProducts.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.modules.basicProducts.entity.BasicProducts;
import org.jeecg.modules.basicProducts.entity.BasicPartsDetail;
import org.jeecg.modules.basicProducts.mapper.BasicPartsDetailMapper;
import org.jeecg.modules.basicProducts.mapper.BasicProductsMapper;
import org.jeecg.modules.basicProducts.service.IBasicProductsService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.*;

/**
 * @Description: 制品
 * @Author: jeecg-boot
 * @Date:   2025-02-10
 * @Version: V1.0
 */
@Service
public class BasicProductsServiceImpl extends ServiceImpl<BasicProductsMapper, BasicProducts> implements IBasicProductsService {

	@Autowired
	private BasicProductsMapper basicProductsMapper;
	@Autowired
	private BasicPartsDetailMapper basicPartsDetailMapper;
	
	@Override
	@Transactional
	public void saveMain(BasicProducts basicProducts, List<BasicPartsDetail> basicPartsDetailList) {
		basicProductsMapper.insert(basicProducts);
		if(basicPartsDetailList!=null && basicPartsDetailList.size()>0) {
			for(BasicPartsDetail entity:basicPartsDetailList) {
				//外键设置
				entity.setProductsId(basicProducts.getId());
				basicPartsDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(BasicProducts basicProducts,List<BasicPartsDetail> basicPartsDetailList) {
		basicProductsMapper.updateById(basicProducts);
		
		//1.先删除子表数据
		basicPartsDetailMapper.deleteByMainId(basicProducts.getId());
		
		//2.子表数据重新插入
		if(basicPartsDetailList!=null && basicPartsDetailList.size()>0) {
			for(BasicPartsDetail entity:basicPartsDetailList) {
				//外键设置
				entity.setProductsId(basicProducts.getId());
				basicPartsDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		basicPartsDetailMapper.deleteByMainId(id);
		basicProductsMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			basicPartsDetailMapper.deleteByMainId(id.toString());
			basicProductsMapper.deleteById(id);
		}
	}

	@Override
	public BasicProducts getByProductsSku(String sku) {
		LambdaQueryWrapper<BasicProducts> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(BasicProducts::getProductsSku, sku);
		return baseMapper.selectOne(wrapper);
	}

	@Override
	public BasicProducts getByProductsBack(String back) {
		LambdaQueryWrapper<BasicProducts> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(BasicProducts::getProductsBack, back);
		return baseMapper.selectOne(wrapper);
	}

	@Override
	public Map<String, BasicProducts> getByProductsSkus(Set<String> productsSkus) {
		LambdaQueryWrapper<BasicProducts> wrapper = new LambdaQueryWrapper<>();
		wrapper.in(BasicProducts::getProductsSku, productsSkus);
		List<BasicProducts> basicProductsList = list(wrapper);

		Map<String, BasicProducts> basicProductsMap = new HashMap<>();
		for (BasicProducts basicProduct : basicProductsList) {
			basicProductsMap.put(basicProduct.getProductsSku(), basicProduct);
		}
		return basicProductsMap;
	}

}
